class PositionAdd{
    constructor(container){
        this.container=container;
        this.createDOM().then($.proxy(this.init,this));
    }
    createDOM(){
        return new Promise(resolve=>{
            this.container.load("/html/component/list/positionAdd.html",resolve);
        })
    }
    init(){
    this.positionInput = $("#positionNameInput");
    this.companyInput = $("#companyInput");
    this.salaryInput = $("#salaryInput");
    this.logoInput=$("#logoInput");
    this.addBtn = $("#add-position-btn");
    this.succ = $("#add-succ");
    this.fail = $("#add-fail");
    this.closeBtn = $("#add-position-close");
    this.addPosition();
    }
    addPosition () {
        this.addBtn.on("click", $.proxy(this.handleAddBtnClick, this));
        
    }
    handleAddBtnClick () {
        // let position = this.positionInput.val(),
        //   company = this.companyInput.val(),
        //   salary = this.salaryInput.val();
        // ---表单验证------

        // FormData向服务器传输文件用的对象
        let formdata =new FormData();
        formdata.append("position",this.positionInput.val());
        formdata.append("company",this.companyInput.val());
        formdata.append("salary",this.salaryInput.val());
        formdata.append("logo",this.logoInput.get(0).files[0]);


        $.ajax({
          url:baseUrl+ "/position/add",
          method:"POST",
          // 告诉jquery不用再处理数据了，因为formdata已经处理好了
          processData:false,
          //告诉jquery不用再处理content-type了
          contentType:false,
          data:formdata,
          success:$.proxy(this.handleAddPositionSucc, this)
        });
        // 向服务器发送新增请求
        // $.get(baseUrl+ "/position/add", {position, company, salary}, $.proxy(this.handleAddPositionSucc, this));
    }
    handleAddPositionSucc(res){
        if(res.res_code === 1){
            this.succ.html(res.res_message).removeClass("hide");
            setTimeout(() => {
              this.succ.addClass("hide");
              this.closeBtn.trigger(new $.Event("click"));
            },1500);
          }else{
            this.fail.html(res.res_message).removeClass("hide");
            setTimeout(() => {
              this.fail.addClass("hide");
            },1500);
          }
    }
}